package com.ygg.bootservice.service.impl;

import com.alibaba.druid.util.StringUtils;
import com.ygg.bootcommon.model.dao.TokenDao;
import com.ygg.bootcommon.model.entity.Token;
import com.ygg.bootcommon.utils.CommonUtils;
import com.ygg.bootservice.service.ITokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author ：longyang
 * @date ：Created in 2020/12/15 9:36
 * @description：令牌service实现类
 */
@Service
@Async("taskExecutor")
@Transactional
public class TokenServiceImpl implements ITokenService {
    @Autowired
    private TokenDao tokenDao;
    /**
     * 申请令牌
     *
     * @param map
     * @return
     */
    @Override
    public String getToken(Map<String, String> map) throws Exception {
        String userId = map.get("userId");//用户登录账号
        String siteCode = map.get("siteCode");//工厂编码
        String workCenterCode = map.get("workCenterCode");//工作中心编码
        String token = UUID.randomUUID().toString().toUpperCase().replaceAll("-","")
                + "|" + userId + "|"
                +  Calendar.getInstance().getTimeInMillis();
        Token tokenEntity = new Token();
        tokenEntity.setCode(token);
        tokenEntity.setCreateId(userId);
        tokenEntity.setCreateDate(new Date());
        tokenEntity.setModifyId(userId);
        tokenEntity.setModifyDate(new Date());
        tokenEntity.setSiteCode(siteCode);
        tokenEntity.setWorkCenterCode(workCenterCode);
        //将新生成的令牌号写入令牌表
        tokenDao.insertToken(tokenEntity);
        //返回生成的令牌
        return token;
    }

    /**
     * 获取所有已经生成的令牌
     * 传入区间时则查询区间内的数据，没传区间则查询所有的数据
     *
     * @return
     */
    @Override
    public List<Token> getAllToken(Map<String,String> map) throws Exception {
        Integer start = map.get("start") == null ? 0
                : Integer.parseInt(map.get("start"));
        Integer end = map.get("end") == null ? 0
                : Integer.parseInt(map.get("end"));
        List<Token> list = tokenDao.getAllToken();
        if(start == 0 && end == 0){
            return list;
        }
        List<Token> newList = CommonUtils.getLimitList(list,start,end);
        return newList;
    }

    /**
     * 更新令牌
     * @param map
     */
    @Override
    public void updateToken(Map<String,String> map) throws Exception {
        String userId = map.get("userId");//修改人
        String code = map.get("code");//令牌编码
        if(StringUtils.isEmpty(code)){
            throw new Exception("令牌编码不能为空！");
        }
        Integer type = map.get("type") == null ? 0 : Integer.parseInt(map.get("type"));//令牌类型
        List<Token> tokenList = tokenDao.getTokenByCode(code);
        if(tokenList.isEmpty()){
            throw new Exception("未找到编码为："+code+"，的令牌，请检查数据是否被删除！");
        }
        Token token = tokenList.get(0);
        token.setModifyId(userId);
        token.setModifyDate(new Date());
        token.setType(type);
        tokenDao.updateToken(token);
    }
}
